// 巡查计划
import store from '../index'
import useConfirm from '@/hooks/useConfirm'
import { InspectionPlanModel, InspectResultkModel } from '@/models'
const inspectionPlanModel = new InspectionPlanModel()
const inspectResultk = new InspectResultkModel()
const defaultState = () => {
    return {
        transformData: {
            pass: true,
            list: []
        },
        detailBaseInfo: {},
        detailRecord: {}
    }
}

const getters = {}

const mutations = {
    setTransformData(state, transformData) {
        state.transformData = transformData
    },
    setDetailBaseInfo(state, detailBaseInfo) {
        state.detailBaseInfo = detailBaseInfo
    },
    setDetailRecord(state, detailRecord) {
        state.detailRecord = detailRecord
    }
}

const actions = {
    async saveOrUpdate({ commit }, data) {
        const res = await useConfirm({
            text: '是否确定保存表单',
            model: inspectionPlanModel,
            modelAction: 'saveOrUpdate',
            data
        })
        return res
    },
    async delete({ commit }, data) {
        const res = await useConfirm({
            text: '是否确定删除表单',
            model: inspectionPlanModel,
            modelAction: 'delete',
            data
        })
        return res
    },
    async submit({ commit }, data) {
        const res = await useConfirm({
            text: '是否确定提交表单',
            model: inspectionPlanModel,
            modelAction: 'submit',
            data
        })
        return res
    },
    async recall({ commit }, data) {
        const res = await useConfirm({
            text: '是否确定撤回表单',
            model: inspectionPlanModel,
            modelAction: 'recall',
            data
        })
        return res
    },
    async stop({ commit }, data) {
        const res = await useConfirm({
            text: '是否确定中止表单',
            model: inspectionPlanModel,
            modelAction: 'stop',
            data
        })
        return res
    },
    async start({ commit }, data) {
        const res = await useConfirm({
            text: '是否确定开启表单',
            model: inspectionPlanModel,
            modelAction: 'start',
            data
        })
        return res
    },
    async detail({ commit }, { number, edit }) {
        try {
            store.commit('openLoading')
            const res = await inspectionPlanModel.detail({ number, isEdit: edit }, edit === false ? 'filter' : null)
            commit('setDetailBaseInfo', res)
            return res
        } finally {
            store.commit('closeLoading')
        }
    },
    async getPlanInspections({ commit }, id) {
        try {
            store.commit('openLoading')
            const res = await inspectResultk.getPlanInspections({ id })
            commit('setDetailRecord', inspectResultk.transformNull(res))
            return res
        } finally {
            store.commit('closeLoading')
        }
    }
}

export default {
    namespaced: true,
    state: defaultState(),
    getters,
    mutations,
    actions
}
